/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxRegister
 * @Package com.rx.core.base
 * @Description: 自定义注册
 * @author: 陈锦韬
 * @date: 2021\6\11 0011
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.base;

import com.rx.core.util.EmptyChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Administrator
 * @Description: 自定义注册
 * @date: 2021\6\11 0011
 */
@Slf4j
abstract public class RxRegister {
    public Environment environment;
    public ResourceLoader resourceLoader;

    protected String[] getPackages(AnnotationMetadata importingClassMetadata,String defalutPkg,String annoName) {

        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(annoName));
        String[] basePackages = null;
        boolean hasDefaultPkg = false;
        if(EmptyChecker.notEmpty(annoAttrs)){
            //扫描注解
            List<String> basePackageList = new ArrayList<>();
            for (String pkg : annoAttrs.getStringArray(RxBaseConstant.ATTRIBUTES_VALUE)) {
                if (StringUtils.hasText(pkg)) {
                    basePackageList.add(pkg);
                }
                if(defalutPkg.equalsIgnoreCase(pkg)){
                    hasDefaultPkg = true;
                }
            }
            if (!hasDefaultPkg){
                basePackageList.add(defalutPkg);
            }
            //TODO:从配置获取扫描包

            if(EmptyChecker.notEmpty(basePackageList)){
                basePackages = new String[basePackageList.size()];
                basePackageList.toArray(basePackages);
            }
        }

        if(EmptyChecker.isEmpty(basePackages)){
            if (EmptyChecker.isEmpty(RxBaseSpringApplication.componentScan)){
                basePackages=RxBaseSpringApplication.basePackages;
            }else {
                basePackages = RxBaseSpringApplication.componentScan.basePackages();
            }
        }
        return basePackages;

    }
}
